<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="logo-wrapper">
          <!-- <img src="@/assets/logo.svg" alt="Logo" class="logo"> -->
          <div alt="Logo" class="title-icon">🐰</div>
          <h1 class="system-title">爱吃兔溯源管理系统</h1>
        </div>
        <h2 class="login-title">欢迎登录</h2>
      </div>
      
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px" class="login-form">
        <el-form-item label="用户名" prop="userName" class="form-item">
          <el-input 
            v-model="form.name" 
            placeholder="请输入用户名"
            autocomplete="username"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item label="密码" prop="userPass" class="form-item">
          <el-input 
            v-model="form.pwd" 
            type="password" 
            show-password
            placeholder="请输入密码"
            autocomplete="current-password"
            prefix-icon="Lock"
            @keyup.enter="onSubmit"
          />
        </el-form-item>

        <el-form-item class="form-item remember-item">
          <el-checkbox v-model="rememberPassword" class="remember-checkbox">记住密码</el-checkbox>
        </el-form-item>

        <el-form-item class="form-item">
          <el-button 
            type="primary" 
            @click="onSubmit" 
            :loading="loading" 
            :disabled="loading"
            class="login-button"
            style="width: 100%"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="third-party-login">
        <div class="divider">
          <span>其他方式登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const loading = ref(false)
const rememberPassword = ref(false)

const form = reactive({
  name: '',
  pwd: '',
})

const rules = {
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
}

// 登录提交
const onSubmit = ()=> {
   axios({
        url:"http://localhost:5028/api/Goods/Login",
        method:"post",
        params:form
    })
    .then(res=>{
        console.log(res);
        
        if(res.data.code==100)
        {
             // 保存用户信息
            localStorage.setItem("UserName",res.data.data.userName);
            localStorage.setItem("RoleName",res.data.data.roleName);
            router.push("/home");
            ElMessage.success(res.data.message);
        }
        else{
            ElMessage.error(res.data.message);
        }
       
    })
}

// 页面加载时检查是否有记住的用户信息
onMounted(() => {
  const rememberedUser = window.localStorage.getItem('rememberedUser')
  if (rememberedUser) {
    try {
      const userData = JSON.parse(atob(rememberedUser))
      form.name = userData.userName || ''
      form.pwd = userData.userPass || ''
      rememberPassword.value = true
    } catch (e) {
      // 解密或解析失败时忽略
      window.localStorage.removeItem('rememberedUser')
    }
  }
  
})
</script>

<style lang="css" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #4c5eaf 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  transition: all 0.3s ease;
  overflow: hidden;
  position: relative;
}

/* 添加装饰效果 */
.login-card::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, rgba(102, 126, 234, 0.05), transparent);
  transform: rotate(30deg);
  z-index: 0;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
  z-index: 1;
}

.logo-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.logo {
  width: 50px;
  height: 50px;
  margin-right: 15px;
  animation: logoFloat 2s ease-in-out infinite;
}
  .title-icon {
      font-size: 5rem;
  }
  .title-icon {
    font-size: 5rem;
    margin-bottom: 15px;
    display: block;
    animation: logoFloat 2s ease-in-out infinite;
}

@keyframes logoFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.system-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #409eff;
  margin: 0;
}

.login-form {
  position: relative;
  z-index: 1;
}

.form-item {
  margin-bottom: 24px;
}

.remember-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.remember-checkbox {
  color: #606266;
}

.forgot-password {
  color: #409eff;
  font-size: 14px;
}

.login-button {
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

/* 登录按钮悬停效果 */
.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: all 0.6s ease;
}

.login-button:hover::before {
  left: 100%;
}

.third-party-login {
  margin-top: 30px;
  position: relative;
  z-index: 1;
}

.divider {
  text-align: center;
  margin-bottom: 20px;
  position: relative;
  color: #909399;
  font-size: 14px;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: calc(50% - 50px);
  height: 1px;
  background: #e4e7ed;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 30px;
}

.social-icon {
  font-size: 28px;
  color: #909399;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 10px;
  border-radius: 50%;
  background: #f5f7fa;
}

.social-icon:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 微信图标悬停效果 */
.social-icon:nth-child(1):hover {
  color: #07c160;
  background: rgba(7, 193, 96, 0.1);
}

/* QQ图标悬停效果 */
.social-icon:nth-child(2):hover {
  color: #12b7f5;
  background: rgba(18, 183, 245, 0.1);
}

/* 微博图标悬停效果 */
.social-icon:nth-child(3):hover {
  color: #e6162d;
  background: rgba(230, 22, 45, 0.1);
}

/* 动画效果 */
@keyframes cardAppear {
  from {
    opacity: 0;
    transform: translateY(-50px) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.login-card {
  animation: cardAppear 0.8s ease-out;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 10px;
  }
  
  .login-card {
    margin: 10px;
  }
  
  .social-icons {
    gap: 20px;
  }
  
  .social-icon {
    font-size: 24px;
    padding: 8px;
  }
}

@media (max-width: 480px) {
  .logo {
    width: 40px;
    height: 40px;
  }
  
  .system-title {
    font-size: 20px;
  }
  
  .login-title {
    font-size: 24px;
  }
}
</style>